﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using FirstView.Models;
using PublishingHouse.Services;
using FirstView.Entities;

namespace FirstView.Controllers
{
    public class CustomerController : Controller
    {
        protected CustomerService customerService = new CustomerService();
        public IActionResult Index()
        {
            var getCookie = "";
            HttpContext.Request.Cookies.TryGetValue("getCookie", out getCookie);
            var customer = customerService.Query(getCookie);
            return View(customer);
        }
        public IActionResult Information()
        {
            var getCookie = "";
            HttpContext.Request.Cookies.TryGetValue("getCookie", out getCookie);
            if(getCookie==null)
            {
                return Redirect(Url.Action("Index", "Home"));
            }
            var customer = customerService.Query(getCookie);
            return View(customer);
        }
        public IActionResult ModifyJudge(string name, string sex, int age, string number, string email, string address)
        {
            var getCookie = "";
            HttpContext.Request.Cookies.TryGetValue("getCookie", out getCookie);
            if (getCookie == null)
            {
                return Redirect(Url.Action("Index", "Home"));
            }
            var customer = customerService.Query(getCookie);
            customer.Name = name;
            customer.Sex = sex;
            customer.Age = age;
            customer.Phone = number;
            customer.Mail = email;
            customer.Address = address;
            customerService.Update(customer);
            return Redirect(Url.Action("Information"));
        }
        public IActionResult BookInformation()
        {
            var getCookie = "";
            HttpContext.Request.Cookies.TryGetValue("getCookie", out getCookie);
            if (getCookie == null)
            {
                return Redirect(Url.Action("Index", "Home"));
            }
            var needbook = customerService.NeedbookList(getCookie);
            return View(needbook);
        }
        public IActionResult BookDetail(string id)
        {
            var getCookie = "";
            HttpContext.Request.Cookies.TryGetValue("getCookie", out getCookie);
            if (getCookie == null)
            {
                return Redirect(Url.Action("Index", "Home"));
            }
            var book = customerService.BookDetail(id,getCookie);
            return View(book);
        }
        public IActionResult OrderInformation()
        {
            var getCookie = "";
            HttpContext.Request.Cookies.TryGetValue("getCookie", out getCookie);
            if (getCookie == null)
            {
                return Redirect(Url.Action("Index", "Home"));
            }
            var customerorder = customerService.OrderList(getCookie);
            return View(customerorder);
        }
        public IActionResult AddOrder( string isbn, string address, decimal number)
        {
            var getCookie = "";
            HttpContext.Request.Cookies.TryGetValue("getCookie", out getCookie);
            if (getCookie == null)
            {
                return Redirect(Url.Action("Index", "Home"));
            }
            if (number == 0)
            {
                return Json(new
                {
                    code = 404,
                    msg = "你还没有告诉我们您要买多少本呢！",
                });
            }
            if (address == null)
            {
                return Json(new
                {
                    code = 120,
                    msg = "您还没有告诉我们您要寄到哪去呢！",
                });
            }
            
            if(!customerService.AddOrder(getCookie, isbn, address, number))
            {
                return Json(new
                {
                    code = 500,
                    msg = "我们的库存不够哦！",
                });
            }
            return Json(new
            {
                code = 200,
                msg = "下单成功，请在订单中心查看！",
            });
        }
        public IActionResult ReturnBook(int id)
        {
            customerService.ReturnBook(id);
            return Redirect(Url.Action("OrderInformation"));
        }
        public IActionResult ReceiveBook(int id)
        {
            customerService.ReceiveBook(id);
            return Redirect(Url.Action("OrderInformation"));
        }
        public IActionResult ForeignLongStory(string id)
        {
            var getCookie = "";
            HttpContext.Request.Cookies.TryGetValue("getCookie", out getCookie);
            if (getCookie == null)
            {
                return Redirect(Url.Action("Index", "Home"));
            }
            var BookType = customerService.BookTypeList(getCookie,id);
            return View(BookType);
        }
        public IActionResult ChinaStory(string id)
        {
            var getCookie = "";
            HttpContext.Request.Cookies.TryGetValue("getCookie", out getCookie);
            if (getCookie == null)
            {
                return Redirect(Url.Action("Index", "Home"));
            }
            var BookType = customerService.BookTypeList(getCookie, id);
            return View(BookType);
        }
        public IActionResult ForeignStory(string id)
        {
            var getCookie = "";
            HttpContext.Request.Cookies.TryGetValue("getCookie", out getCookie);
            if (getCookie == null)
            {
                return Redirect(Url.Action("Index", "Home"));
            }
            var BookType = customerService.BookTypeList(getCookie, id);
            return View(BookType);
        }
        public IActionResult ChangePwd(string oldPwd, 
            string newPwd)
        {
            var getCookie = "";
            HttpContext.Request.Cookies.TryGetValue("getCookie",
                out getCookie);
            if (getCookie == null)
            {
                return Redirect(Url.Action("Index", "Home"));
            }
            var customer = customerService.Query(getCookie);
            if (oldPwd == newPwd)
            {
                return Json(new
                {
                    code = 222,
                    msg = "新旧密码不能一样",
                });
            }
            if (customer.Password != oldPwd)
            {
                return Json(new
                {
                    code = 503,
                    msg = "旧密码不正确！请检查后输入！",
                });
            }
            else if (customer.Password == oldPwd)
            {
                customer.Password = newPwd;
                customerService.Update(customer);
                return Json(new
                {
                    code = 200,
                    msg = "修改成功！",
                });
            }
            return Json(new
            {
                code = 2050,
                msg = "服务器异常！",
            });
        }
        public IActionResult ModifyPassword()
        {
            var getCookie = "";
            HttpContext.Request.Cookies.TryGetValue("getCookie", out getCookie);
            if (getCookie == null)
            {
                return Redirect(Url.Action("Index", "Home"));
            }
            var customer = customerService.Query(getCookie);
            return View(customer);
        }
        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
